<?php

namespace Admin\Controller;

use Think\Controller;

class AdController extends CommonController
{
    public function lst()
    {
        $ad=D('ad');
        $res=$ad->query("SELECT * FROM ad");
        $this->assign('res',$res);
        $this->display();
    }

    public function add()//添加
    {
        $ad=D('ad');
        if (IS_POST){
            $data['title']=I('post.title');
            $data['pic']=I('post.pic');
            $data['url']=I('post.url');
            $data['desc']=I('post.desc');
            $data['comment']=I('post.comment');

            if ($_FILES['pic']['tmp_name'] != '') {
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 3145728;// 设置附件上传大小
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = $_SERVER['DOCUMENT_ROOT']; // 设置附件上传根目录
                $upload->savePath = '/Public/Uploads/'; // 设置附件上传（子）目录
                // 上传单个文件
                $info = $upload->uploadOne($_FILES['pic']);
                if (!$info) {
                    $this->error($upload->getError());// 上传错误提示错误信息
                } else {
                    $data['pic'] = $info['savepath'] . $info['savename'];// 上传成功 获取上传文件信息
                }
            } else {

            }
            if ($ad->create($data)){
                if ($ad->add()){
                    $this->success('广告添加成功！',U('lst'));
                }else{
                    $this->error('广告添加失败！');
                }
            }else{
                    $this->error($ad->getError());
            }

            return;
        }
        $this->display();
    }
    public function edit()//修改
    {
        $ad=D('ad');
//        $id=I('id');
//        $res=$ad->query("SELECT * FROM ad WHERE id=$id");
        $res=$ad->find(I('id'));
        $this->assign('res',$res);
        if (IS_POST){
            $data['id']=I('post.id');
            $data['title']=I('post.title');
            $data['url']=I('post.url');
            $data['desc']=I('post.desc');
            $data['comment']=I('post.comment');
            $data['pic']=I('post.pic');
            if ($_FILES['pic']['tmp_name'] != '') {
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 3145728;// 设置附件上传大小
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = $_SERVER['DOCUMENT_ROOT']; // 设置附件上传根目录
                $upload->savePath = '/Public/Uploads/'; // 设置附件上传（子）目录
                // 上传单个文件
                $info = $upload->uploadOne($_FILES['pic']);
                if (!$info) {
                    $this->error($upload->getError());// 上传错误提示错误信息
                } else {
                    $data['pic'] = $info['savepath'] . $info['savename'];// 上传成功 获取上传文件信息
                }
            } else {

            }
            if ($ad->create($data)){
                if ($ad->save()){
                    $this->success('广告修改成功！',U('lst'));
                }else{
                    $this->error('广告修改失败！');
                }
            }else{
                $this->error($ad->getError());
            }

            return;
        }

        $this->display();
    }
    public function del()//删除
    {
        $ad=D('ad');
        if ($ad->delete(I('id'))){
            $this->success('删除成功！',U('lst'));
        }else{
            $this->error('删除失败！');
        }

    }

}

